﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

#if !NETCOREAPP
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
    using System.Web.Mvc.Html;

#endif

#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Kugar.Core.Web;
#endif

namespace Kugar.MVC.MobileUI.WeUI.PageControls.PageCommons
{
    public class PageCommon_FlexLayoutContainer : ContainerBase
    {
        public PageCommon_FlexLayoutContainer(ViewContext viewContext) : base(viewContext)
        {
            _attrs.Class("weui-flex");
        }

        public FlexItem RenderItem()
        {
            return new FlexItem(ViewContext);
        }

        public MvcHtmlString RenderItem(Func<PageCommon_FlexLayoutContainer, HelperResult> func)
        {
            //ViewContext.Writer.WriteLine();

            var str = $"<div class=\"weui-flex__item\">{func(this).ToHtmlString()}</div>";

            return new MvcHtmlString(str);
        }

        public override void Dispose()
        {
            ViewContext.Writer.WriteLine("</div> <!-- End weui-flex -->");
        }

        internal override ContainerBase RenderInternal()
        {
            ViewContext.Writer.WriteLine($"<div {_attrs}>");

            return this;
        }

        public class FlexItem : ContainerBase
        {
            public FlexItem(ViewContext viewContext) : base(viewContext)
            {
                _attrs.Class("weui-flex__item");
            }

            public override void Dispose()
            {
                ViewContext.Writer.WriteLine("</div> <!-- end  flexitem-->");
            }

            internal override ContainerBase RenderInternal()
            {
                ViewContext.Writer.WriteLine($"<div {_attrs}>");

                return this;
            }
        }
    }

    public class PageCommon_FlexLayout : ControlBase
    {
        private List<string> _items=new List<string>(3);

        public PageCommon_FlexLayout(ViewContext viewContext) : base(viewContext)
        {
            _attrs.Class("weui-flex");
        }

        public PageCommon_FlexLayout RenderItem(Func<PageCommon_FlexLayout, HelperResult> func,bool isRender=true)
        {
            //ViewContext.Writer.WriteLine();

            //var str = $"<div class=\"weui-flex__item\">{func(this).ToHtmlString()}</div>";

            if (isRender)
            {
                _items.Add(func(this).ToHtmlString());
            }
            

            return this;
        }

        public PageCommon_FlexLayout RenderItem(string content, bool isRender = true)
        {
            //ViewContext.Writer.WriteLine();

            //var str = $"<div class=\"weui-flex__item\">{content}</div>";

            if (isRender)
            {
                _items.Add(content);
            }
                

            return this;
        }

        public override MvcHtmlString Render()
        {
            ViewContext.Writer.WriteLine($"<div {_attrs}>");

            foreach (var item in _items)
            {
                ViewContext.Writer.WriteLine("<div class=\"weui-flex__item\">");

                ViewContext.Writer.WriteLine(item);

                ViewContext.Writer.WriteLine("</div> <!-- end flex__item -->");
            }
            
            ViewContext.Writer.WriteLine("</div> <!-- End weui-flex -->");

            return MvcHtmlString.Empty;
        }
    }
}